package org.grow.commodity.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.grow.commodity.dao.CommodityEntityDao;
import org.grow.commodity.dao.StorageDao;
import org.grow.commodity.entity.*;
import org.grow.commodity.exception.CommodityException;
import org.grow.commodity.util.CodeGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.regex.Pattern;

/**
 * @Author: xwg
 * @CreateDate: 21-8-6
 */

@RestController
@RequestMapping("/commodity/commodityEntity")
public class CmBasicController {
    @Autowired
    private CommodityEntityDao commodityEntityDao;
    @Autowired
    private StorageDao storageDao;
    @Resource
    private ThreadLocal<CodeGenerator> codeGeneratorTemplate;

    //18300284060
    @PostMapping("/")
    public String CmEntityInsert(@RequestBody CommodityEntity commodityEntity) {


        System.out.println(commodityEntity);
//        校验传过来的数据是否合法
        String barCode = commodityEntity.getBarCode();
        //language=RegExp
        String barCodePattern = "\\d{8,10}";
        boolean matches = Pattern.matches(barCodePattern, barCode);
        if (!matches) {
            throw new CommodityException("条形码只能是8-10位", 810);
        }

//        后端根据业务逻辑设置数据
        commodityEntity.setCommodityId(-1);
        commodityEntity.setIsDeleted(false);
        String code = codeGeneratorTemplate.get().nextCode(CommodityConst.CommodityEntity);
        commodityEntity.setCommodityCode(code);

        int insert = commodityEntityDao.insert(commodityEntity);

        Storage storage = new Storage();
        storage.setCommodityFk(commodityEntity.getCommodityCode());
        storage.setCmDescr(commodityEntity.getCommodityDescr());
        storage.setActualStock(0);
        storage.setSoldStock(0);
        storage.setIntentionStock(0);
        storage.setVersion(0);
        storage.setIsDeleted(false);
        storageDao.insert(storage);
//       todo:保存完商品表数据 配套初始化库存表数据
        return commodityEntity.getCommodityCode();
    }

    @GetMapping("/{code}")
    public CommodityEntity getCommodity(@PathVariable("code") String code) {
        CommodityEntity ce = commodityEntityDao.findByCommodityCode(code);
        if (ce == null ) {
            throw new CommodityException("商品信息不存在: "+code,810);
        }
        return ce;
    }

    @PostMapping("/query")
    public PagedResults<CommodityEntity> queryCommodityEntity(
            @RequestBody CmEntitySearchForm cmEntitySearchForm){
        Integer pageNum = cmEntitySearchForm.getPageNum();
        Integer pageSize = cmEntitySearchForm.getPageSize();
        Page<CommodityEntity> page = new Page<>(pageNum, pageSize);
        CmEntityQueryCondition qc = cmEntitySearchForm.getQueryCondition();
        commodityEntityDao.dynamicQuery(page, qc);
        PagedResults<CommodityEntity> pagedResults = new PagedResults<>();
        pagedResults.setResults(page.getRecords());
        pagedResults.setTotal(page.getTotal());
        return pagedResults;

    }

    @GetMapping("/find")
    public List<CommodityEntity> findByNameOrCode(@RequestParam("nameOrCode") String nc){
       return commodityEntityDao.findByNameOrCode(nc);
    }
}
